package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbHousekeeper;
import com.bwie.service.TbHousekeeperService;
import com.bwie.mapper.TbHousekeeperMapper;
import com.bwie.utils.Result;
import com.bwie.vo.HouseVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* @author 28242
* @description 针对表【tb_housekeeper(家政服务人员表)】的数据库操作Service实现
* @createDate 2025-07-11 08:58:00
*/
@Service
public class TbHousekeeperServiceImpl extends ServiceImpl<TbHousekeeperMapper, TbHousekeeper>
    implements TbHousekeeperService{

    @Autowired
    private TbHousekeeperMapper tbHousekeeperMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result HouseLogin(HouseVo houseVo) {
        //判断客户账号是否唯一
        QueryWrapper<TbHousekeeper> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbHousekeeper::getHouseName,houseVo.getHouseName());

        TbHousekeeper tbHousekeeper = tbHousekeeperMapper.selectOne(wrapper);
        if (tbHousekeeper==null){
            return Result.error().setMsg("用户不存在");
        }
        //判断密码是否错误
        if (!tbHousekeeper.getHousePass().equals(houseVo.getHousePass())){
            return Result.error().setMsg("密码错误");
        }

        String redisToken="token-"+tbHousekeeper.getHouseId();

        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("houseId", tbHousekeeper.getHouseId())
                .claim("houseName", tbHousekeeper.getHouseName())
                .compact();

        redisTemplate.opsForValue().set(redisToken,token,5, TimeUnit.MINUTES);

        tbHousekeeper.setToken(token);
        tbHousekeeper.setHousePass(null);


        return Result.ok().setData(tbHousekeeper);
    }
}




